//
//  WCG_O_CollectionViewCell.m
//  Project_B
//
//  Created by 黑黑 on 15/11/24.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_O_CollectionViewCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
@interface WCG_O_CollectionViewCell ()

@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) UILabel *titleLable;
@property (nonatomic, retain) UILabel *subtitleLable;
@property (nonatomic, retain) UIView *coverView;

@end

@implementation WCG_O_CollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.image = [[UIImageView alloc] init];
        [self.contentView addSubview:self.image];
    
        self.coverView = [[UIView alloc] init];
        self.coverView.backgroundColor = [UIColor blackColor];
        self.coverView.alpha = 0.4;
        [self.contentView addSubview:self.coverView];
        
        self.titleLable = [[UILabel alloc] init];
        self.titleLable.backgroundColor = [UIColor clearColor];
        self.titleLable.textColor = [UIColor whiteColor];
        self.titleLable.font = [UIFont fontWithName:@"FZSongKeBenXiuKaiS-R-GB" size:23];
        self.titleLable.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLable];
        
        self.subtitleLable = [[UILabel alloc] init];
        self.subtitleLable.font = [UIFont systemFontOfSize:10];
        self.subtitleLable.textColor = [UIColor whiteColor];
        self.subtitleLable.textAlignment = NSTextAlignmentCenter;
        self.subtitleLable.backgroundColor = [UIColor clearColor];
        [self addSubview:self.subtitleLable];
    }
    return self;
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.image.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    self.coverView.frame = CGRectMake(0, 0, layoutAttributes.size.width, layoutAttributes.size.height);
    self.titleLable.frame = CGRectMake(0, 0, layoutAttributes.size.width, layoutAttributes.size.height);
    self.titleLable.center = self.image.center;
    [self.subtitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.image.mas_bottom).with.offset(-10);
        make.width.mas_equalTo(layoutAttributes.size.width);
        make.height.mas_equalTo(20);
    }];
}

- (void)setSpecialModel:(GXSpecialModel *)specialModel
{
    if (_specialModel != specialModel) {
        _specialModel = specialModel;
    }
    self.titleLable.text = specialModel.title;
    NSString *temp = [NSString stringWithFormat:@"%@人订阅 | %@篇", _specialModel.subscriber_num, _specialModel.content_num];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:temp];
    NSRange subRange = [temp rangeOfString:[self.specialModel.subscriber_num stringValue]];
    [string addAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:subRange];
    NSMutableAttributedString *finalString = [[NSMutableAttributedString alloc] initWithAttributedString:string];
    
    NSRange conRange = [temp rangeOfString:[self.specialModel.content_num stringValue]];
    [finalString addAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:conRange];

    self.subtitleLable.attributedText = finalString;
    
    [self.image sd_setImageWithURL:_specialModel.thumb[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
//    UIImage *tempimage = [self getBrighterImage:self.image.image];
//    self.image.image = tempimage;
}

- (UIImage*) getBrighterImage:(UIImage *)originalImage
{
    UIImage *brighterImage;
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:originalImage.CGImage];
    
    CIFilter *lighten = [CIFilter filterWithName:@"CIColorControls"];
    [lighten setValue:inputImage forKey:kCIInputImageKey];
    [lighten setValue:@(- 0.15) forKey:@"inputBrightness"];
    
    CIImage *result = [lighten valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
    brighterImage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    
    return brighterImage;
}
@end
